package com.sanji.sjmallmodule.order;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.sanji.sjmallapp.BaseActivity;
import com.sanji.sjmallapp.R;
import com.sanji.sjmallconfig.ConfigConts;
import com.sanji.sjmallutils.Tools;
import com.sanji.sjmallwidget.MyListView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 订单详情
 * Created by zsf on 17-1-15.
 */

public class MyOrderDestActivity extends BaseActivity implements View.OnClickListener {
    @BindView(R.id.common_back_img)
    ImageView mCommonBackImg;
    @BindView(R.id.common_left_tv)
    TextView mCommonLeftTv;
    @BindView(R.id.common_right_img)
    ImageView mCommonRightImg;
    @BindView(R.id.my_order_dest_paid_statue)
    TextView mMyOrderDestPaidStatue;
    @BindView(R.id.my_order_dest_phone_tv)
    TextView mMyOrderDestPhoneTv;
    @BindView(R.id.my_order_dest_rever_tv)
    TextView mMyOrderDestReverTv;
    @BindView(R.id.my_order_dest_rever_phone_tv)
    TextView mMyOrderDestReverPhoneTv;
    @BindView(R.id.my_order_dest_rever_address_tv)
    TextView mMyOrderDestReverAddressTv;
    @BindView(R.id.my_order_dest_orderno_tv)
    TextView mMyOrderDestOrdernoTv;
    @BindView(R.id.my_order_dest_buy_date_tv)
    TextView mMyOrderDestBuyDateTv;
    @BindView(R.id.my_order_dest_send_date_tv)
    TextView mMyOrderDestSendDateTv;
    @BindView(R.id.my_order_dest_paid_date_tv)
    TextView mMyOrderDestPaidDateTv;
    @BindView(R.id.my_order_dest_paid_type_tv)
    TextView mMyOrderDestPaidTypeTv;
    @BindView(R.id.my_order_dest_goods_list)
    MyListView mMyOrderDestGoodsList;
    @BindView(R.id.my_order_dest_fare_value)
    TextView mMyOrderDestFareValue;
    @BindView(R.id.my_order_dest_point_value)
    TextView mMyOrderDestPointValue;
    @BindView(R.id.my_order_dest_amount_value)
    TextView mMyOrderDestAmountValue;
    @BindView(R.id.my_order_dest_to_pay_btn)
    TextView mMyOrderDestToPayBtn;

    private MyOrderBean order = null;

    private FirmOrderAdapter mOrderAdapter;

    private List<ShopcarChildBean> shopChildList = new ArrayList<>();
    private String receiverMobile;//收货电话
    private String receiverAddress;//收货地址
    private int payType;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_order_dest);
        ButterKnife.bind(this);

        order = (MyOrderBean) getIntent().getSerializableExtra("order");

        initView();

        initData();
    }

    private void initView() {
        mCommonBackImg.setOnClickListener(this);
        mCommonLeftTv.setOnClickListener(this);
        mCommonLeftTv.setVisibility(View.VISIBLE);
        mCommonLeftTv.setText(getResources().getString(R.string.order_details_title));
        mCommonRightImg.setVisibility(View.VISIBLE);
        mCommonRightImg.setImageResource(R.mipmap.fragment_main_noselect);
        receiverMobile = myShare.getString(ConfigConts.MOBILE, null);
        String provice = myShare.getString(ConfigConts.PROVINCIAL_ID, null);
        String detailAddress = myShare.getString(ConfigConts.ADDRESS_STR, null);

        receiverAddress = provice + detailAddress;

        mMyOrderDestPhoneTv.setOnClickListener(this);
        if (order.getOrderStatus() == 3 || order.getOrderStatus() == 4 || order.getShipStatus() != 0 || order.getPaidType() == 1){
            mMyOrderDestToPayBtn.setVisibility(View.GONE);
        }else{
            mMyOrderDestToPayBtn.setOnClickListener(this);
        }
    }

    private void initData() {
        int payStatus = order.getPayStatus();
        payType = order.getPaidType();
        if (payStatus == 0) {
            mMyOrderDestPaidStatue.setText(getResources().getString(R.string.order_status_unpay));
        } else if (payStatus == 1) {
            mMyOrderDestPaidStatue.setText(getResources().getString(R.string.order_status_payed));
        }
        mMyOrderDestReverTv.setText(getResources().getString(R.string.order_receiver) + order.getReceiverName());
        mMyOrderDestReverPhoneTv.setText(getResources().getString(R.string.order_receive_phone) + receiverMobile);
        mMyOrderDestReverAddressTv.setText(getResources().getString(R.string.order_receive_address) + receiverAddress);
        mMyOrderDestOrdernoTv.setText(getResources().getString(R.string.order_receive_no) + order.getOrderNo());
        mMyOrderDestBuyDateTv.setText(getResources().getString(R.string.order_place_time) + order.getBuyDate());
        mMyOrderDestSendDateTv.setText(getResources().getString(R.string.order_delivery_time) + order.getFareDate());
        mMyOrderDestPaidDateTv.setText(getResources().getString(R.string.order_pay_time) + order.getPaidDate());
        if (payType == 0) {
            mMyOrderDestPaidTypeTv.setText(getResources().getString(R.string.order_pay_online));
        } else if (payType == 1) {
            mMyOrderDestPaidTypeTv.setText(getResources().getString(R.string.order_pay_next));
        } else if (payType == 2) {
            mMyOrderDestPaidTypeTv.setText(getResources().getString(R.string.order_pay_pos));
        }
        mMyOrderDestFareValue.setText(String.valueOf(order.getFare()));
        mMyOrderDestPointValue.setText("+" + order.getPoint());
        mMyOrderDestAmountValue.setText(getResources().getString(R.string.price_title) + order.getAmount());

        List<ShopcarChildBean> list = order.getGoodsList();
        for (ShopcarChildBean bean : list) {
            if (!bean.getGoodsType().equals("gift")) {
                shopChildList.add(bean);
            }
        }
        mOrderAdapter = new FirmOrderAdapter(this, shopChildList);
        mMyOrderDestGoodsList.setAdapter(mOrderAdapter);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.common_back_img:
                finish();
                break;
            case R.id.common_left_tv:
                finishActivity();
                break;
            case R.id.my_order_dest_phone_tv:
                //客服电话
                Tools.dialPhone(this, mMyOrderDestPhoneTv.getText().toString());
                break;
            case R.id.my_order_dest_to_pay_btn:
                Intent intent = new Intent(this, OrderPayActivity.class);
                intent.putExtra("actualAmount", order.getAmount());
                intent.putExtra("orderId", order.getOrderId());
                startActivityForResult(intent,100);
                break;

        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (RESULT_OK == resultCode && requestCode == 100){
            setResult(RESULT_OK,null);
            finishActivity();
        }
        super.onActivityResult(requestCode, resultCode, data);
    }
}
